<?php
	class Recall_paper_model extends CI_Model{
		
		function _construct(){
			parent::_construct();
		}
		
		function read_recall_paper($uid=-1,$limit=10,$offset=0,$orderby='creat_time',$orderflag='desc'){
			$this->db->order_by($orderby,$orderflag);//将字段以orderby参数排列,"desc"降序,"asc"升序	
			if($uid!=-1) $this->db->where('u_id',$uid);
			$this->db->where('status',0);//将正常状态的信息取出
			$query=$this->db->get('dy_recall_paper_db',$limit,$offset);
			return $query->result();	
		}
		/*默认提取数据库中'dy_recall_paper_db'表的所有信息，并返回
			输入uid参数返回相应用户创建的卷子信息
			徐尘 编辑于2011-5-30*/
		
		//得到用户上传量
		//2011/6/5由sxjscience更新（原来那个貌似有问题)
		function get_recall_times($uid)
		{
				$this->db->select("j_id");
				$this->db->where("u_id",$uid);
				$query=$this->db->get("dy_recall_paper_db");
				return $query->num_rows();	
		}
		function get_by_id($jid){
			$this->db->from('dy_recall_paper_db');			
			$this->db->where('j_id',$jid);
			return $this->db->get()->result();
		}
		function get_by_uid($uid){
			$this->db->from('dy_recall_paper_db');			
			$this->db->where('u_id',$uid);
			return $this->db->get()->result();
		}
		
			
		function creat_recall_paper($data){
			$this->load->helper('security');
			$this->load->helper('date');
			$data = $this->security->xss_clean($data);//对输入的数据进行过滤
			$data['ip']=$this->input->ip_address();
			$now = time();
			$data['creat_time']=unix_to_human($now,TRUE,'eg');
			return $this->db->insert('dy_recall_paper_db',$data);	
		}
		/*向数据库中的'dy_recall_paper_db'表插入一行，若插入成功返回TURE，否则返回FALSE；
			徐尘 编辑于2011-5-26*/
			
			
		function updata_reacll_paper($data,$jid){
			$this->load->helper('security');
			$data = $this->security->xss_clean($data);//对输入的数据进行过滤
			$this->db->from('dy_recall_paper_db');
			$this->db->where('j_id',$jid);
			return $this->db->update('dy_recall_paper_db',$data);
		}
		/*根据主键值来更新数据,若更新成功，则会返回TRUE，否则返回FALSE；
			徐尘 编辑于2011-5-26*/
		
		
		function delete_recall_paper($jid){
			$this->db->from('dy_recall_paper_db');			
			$this->db->where('j_id',$jid);
			$this->db->set('status', '-1', FALSE);
			return $this->db->update('dy_recall_paper_db');
		}
		/*根据主键值删除数据,并非真正删除，而是将其status属性改变为-1，以示隐藏
			成功则会返回一个大于1的数
			徐尘 编辑于2011-5-26*/
			
		function add_problem_cnt($jid){
			$this->db->from('dy_recall_paper_db');			
			$this->db->where('j_id',$jid);
			$this->db->set('problem_cnt', 'problem_cnt+1', FALSE); 
			return $this->db->update('dy_recall_paper_db');
		}
		/*根据主键找到相应字段的‘problem_cnt’属性，++后更新
		徐尘 编辑于2011-5-27*/

		function add_hit_cnt($jid){
			$this->db->from('dy_recall_paper_db');			
			$this->db->where('j_id',$jid);
			$this->db->set('hit_cnt', 'hit_cnt+1', FALSE); 
			return $this->db->update('dy_recall_paper_db');
		}

		/*function add_good_cnt($jid){
			$this->db->from('dy_recall_paper_db');			
			$this->db->where('j_id',$jid);
			$this->db->set('good_cnt', 'good_cnt+1', FALSE); 
			return $this->db->update('dy_recall_paper_db');
		}*/
		function return_row_cnt(){
			$query = $this->db->query('SELECT * FROM dy_recall_paper_db');
			return $query->num_rows();
		}
		function top_sth($limit=10,$sth='title'){
			$query=$this->recall_paper_model->read_recall_paper(-1,$limit,0,'hit_cnt','desc');
			$i=0;
			foreach($query as $row){
				$top[$i]=$row->$sth;
				$i++;
			}
			return $top;
		}
	}
?>